package com.xxg.renrenuser.dao;

import com.mongodb.client.MongoCollection;
import com.xxg.renrencommon.domain.filedenum.userenum.CashOutStatusEnum;
import com.xxg.renrenuser.dao.bean.UserCashOutInfo;
import com.xxg.renrenuser.domain.query.GetCashOutListQuery;
import org.bson.Document;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Repository;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.List;

import static org.springframework.data.mongodb.core.query.Criteria.where;

/**
 * 提现信息表Dao
 *
 * @author xxg
 * @date 2020/12/10
 */
@Repository
public class UserCashOutInfoDao {
    private final MongoTemplate mongoTemplate;

    public UserCashOutInfoDao(MongoTemplate mongoTemplate) {
        this.mongoTemplate = mongoTemplate;
        createCollection();
    }

    /**
     * 创建collection
     */
    private void createCollection() {
        String collectionName = "userCashOutInfo";
        MongoCollection<Document> collection;
        if (!mongoTemplate.getCollectionNames().contains(collectionName)) {
            collection = mongoTemplate.createCollection(UserCashOutInfo.class);
            createIndex(collection);
        }
    }

    /**
     * 创建索引
     *
     * @param collection 数据集
     */
    private void createIndex(MongoCollection<Document> collection) {

    }


    /**
     * 插入提现记录
     *
     * @param userCashOutInfo 记录信息
     */
    public void insert(UserCashOutInfo userCashOutInfo) {
        userCashOutInfo.setCreateTime(new Date());
        mongoTemplate.insert(userCashOutInfo);
    }

    /**
     * 获取非员工的提现申请记录
     *
     * @param pageNo        页码
     * @param pageSize      页大小
     * @param cashOutStatus 提现状态
     * @param startTime         起始时间
     * @param endTime           结束时间
     * @return 结果
     */
    public List<UserCashOutInfo> findByType(Integer pageNo, Integer pageSize, Integer cashOutStatus, Date startTime, Date endTime) {
        Query query;
        if (cashOutStatus == null) {
            query = new Query(where("userType").is(null));
        } else {
            query = new Query(where("status").is(cashOutStatus).and("userType").is(null));
        }
        if (startTime != null) {
            Criteria criteria = new Criteria();
            criteria.and("createTime").gte(startTime);
            query.addCriteria(criteria);
        }
        if (endTime != null) {
            Criteria criteria = new Criteria();
            criteria.and("createTime").lte(endTime);
            query.addCriteria(criteria);
        }
        long start = pageNo * pageSize;
        query.with(Sort.by(Sort.Order.desc("createTime"))).skip(start).limit(pageSize);
        return mongoTemplate.find(query, UserCashOutInfo.class);
    }


    /**
     * 设置提现申请状态记录为已转账
     *
     * @param ids 记录ID列表
     */
    public void setApplyStatus(List<String> ids) {
        Query query = new Query(where("_id").in(ids));
        Update update = new Update();
        update.set("updateTime", new Date());
        update.set("status", CashOutStatusEnum.DONE.getValue());
        mongoTemplate.updateMulti(query, update, UserCashOutInfo.class);
    }

    public UserCashOutInfo findByUserIdAndStatus(Integer userId, Integer status) {
        Query query = new Query(where("userId").is(userId).and("status").is(status));
        return mongoTemplate.findOne(query, UserCashOutInfo.class);
    }

    public List<UserCashOutInfo> findByCondition(GetCashOutListQuery getCashOutListQuery) {
        long skip = (getCashOutListQuery.getPageNo() - 1) * getCashOutListQuery.getPageSize();
        Integer limit = getCashOutListQuery.getPageSize();
        Query query = buildQueryByCondition(getCashOutListQuery);
        query.skip(skip);
        query.limit(limit);
        return mongoTemplate.find(query, UserCashOutInfo.class);
    }

    private Query buildQueryByCondition(GetCashOutListQuery getCashOutListQuery) {
        Query query = new Query();
        if (getCashOutListQuery.getStatus() != null) {
            query.addCriteria(where("status").is(getCashOutListQuery.getStatus()));
        }
        if (getCashOutListQuery.getNickName() != null && getCashOutListQuery.getNickName().trim().length() > 0) {
            query.addCriteria(where("nickname").is(getCashOutListQuery.getNickName()));
        }
        if (getCashOutListQuery.getBankName() != null && getCashOutListQuery.getBankName().trim().length() > 0) {
            query.addCriteria(where("bankName").is(getCashOutListQuery.getBankName()));
        }
        if (getCashOutListQuery.getOrderId() != null && getCashOutListQuery.getOrderId().trim().length() > 0) {
            query.addCriteria(where("orderIds").is(getCashOutListQuery.getOrderId()));
        }
        if (getCashOutListQuery.getCreateTimeStart() != null && getCashOutListQuery.getCreateTimeEnd() != null) {
            query.addCriteria(where("createTime").lte(getCashOutListQuery.getCreateTimeEnd()).gte(getCashOutListQuery.getCreateTimeStart()));
        }else if (getCashOutListQuery.getCreateTimeStart() != null) {
            query.addCriteria(where("createTime").gte(getCashOutListQuery.getCreateTimeStart()));
        }else if (getCashOutListQuery.getCreateTimeEnd() != null) {
            query.addCriteria(where("createTime").lte(getCashOutListQuery.getCreateTimeEnd()));
        }
        if (getCashOutListQuery.getUpdateTimeEnd() != null && getCashOutListQuery.getUpdateTimeStart() != null) {
            query.addCriteria(where("updateTime").lte(getCashOutListQuery.getUpdateTimeEnd()).gte(getCashOutListQuery.getUpdateTimeStart()));
        }else if (getCashOutListQuery.getCreateTimeStart() != null) {
            query.addCriteria(where("updateTime").gte(getCashOutListQuery.getUpdateTimeStart()));
        }else if (getCashOutListQuery.getCreateTimeEnd() != null) {
            query.addCriteria(where("updateTime").lte(getCashOutListQuery.getUpdateTimeEnd()));
        }
        return query;
    }

    public long countByCondition(GetCashOutListQuery getCashOutListQuery) {
        Query query = buildQueryByCondition(getCashOutListQuery);
        return mongoTemplate.count(query, UserCashOutInfo.class);
    }
}
